package com.tuling.learnmongodb.controller;

import com.alibaba.fastjson.JSON;
import com.tuling.learnmongodb.common.ResponseModel;
import com.tuling.learnmongodb.model.Blog;
import com.tuling.learnmongodb.model.Comment;
import com.tuling.learnmongodb.model.Reply;
import com.tuling.learnmongodb.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/blog")
@Slf4j
public class BlogController {
    @Autowired
    private BlogService blogService;

    @PostMapping("/queryBlogList.do")
    public ResponseModel<List<Blog>> queryBlogList(){
        ResponseModel<List<Blog>> responseModel = new ResponseModel<List<Blog>>();
        responseModel.setData(blogService.queryBlogList());
        return responseModel;
    }


    @PostMapping("/addBlog.do")
    public ResponseModel<Object> addBlog(@RequestBody Blog blog){
        log.info("addBlog param:{}", JSON.toJSONString(blog));
        if(blog == null || StringUtils.isBlank(blog.getContent()) || StringUtils.isBlank(blog.getUserId())
            || StringUtils.isBlank(blog.getUserName())){
            return new ResponseModel<>("00000001", "参数为空");
        }
        ResponseModel<Object> responseModel = new ResponseModel<Object>();
        blogService.addBlog(blog);
        return responseModel;
    }

    @PostMapping("/addBlogComment.do")
    public ResponseModel<Object> addBlogComment(@RequestBody Comment comment){
        log.info("addBlogComment param:{}", JSON.toJSONString(comment));
        if(comment == null || StringUtils.isBlank(comment.getBlogId()) || StringUtils.isBlank(comment.getUserId())
            || StringUtils.isBlank(comment.getUserName()) || StringUtils.isBlank(comment.getContent())){
            return new ResponseModel<>("00000001", "参数为空");
        }
        ResponseModel<Object> responseModel = new ResponseModel<Object>();
        blogService.addBlogComment(comment);
        return responseModel;
    }

    @PostMapping("/addReply.do")
    public ResponseModel<Object> addReply(@RequestBody Reply reply){
        log.info("addBlogComment param:{}", JSON.toJSONString(reply));
        if(reply == null || StringUtils.isBlank(reply.getCommentId()) || StringUtils.isBlank(reply.getUserId())
                || StringUtils.isBlank(reply.getUserName()) || StringUtils.isBlank(reply.getReplyContent())
        || StringUtils.isBlank(reply.getReplyUserId()) || StringUtils.isBlank(reply.getReplyUserName())){
            return new ResponseModel<>("00000001", "参数为空");
        }
        ResponseModel<Object> responseModel = new ResponseModel<Object>();
        blogService.addReply(reply);
        return responseModel;
    }
}
